package com.botarena.client.account;

import com.botarena.client.ui.ErrorDialogBox;
import com.botarena.client.ui.GenericDialogBox;
import com.botarena.client.ui.InformationDialogBox;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;

public class AccountEditorPanelWidget {

	private FormPanel form;
	
	private Hidden keyHidden;
	private Label emailLabel;
	private Label accountLabel;
	private TextBox nameInput;
	private Image avatar;
	//private TextBox urlInput;
	private FileUpload diskInput;
	private CheckBox deleteAvatar;
	
	public AccountEditorPanelWidget(String formAction) {
		FlexTable flexPanel = new FlexTable();
		flexPanel.setStyleName("formTable");
		
		form = new FormPanel();
		form.setAction(formAction);
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		form.setWidget(flexPanel);
		
		flexPanel.setWidget(0, 0, new Label("Google account: "));
		accountLabel = new Label("");
		flexPanel.setWidget(0, 1, accountLabel);
		
		flexPanel.setWidget(1, 0, new Label("Email: "));
		emailLabel = new Label("");
		flexPanel.setWidget(1, 1, emailLabel);
		
		flexPanel.setWidget(2, 0, new Label("Display name:"));
		nameInput = new TextBox();
		nameInput.setName("name");
		flexPanel.setWidget(2, 1, nameInput);
		
		flexPanel.setWidget(3, 0, new Label("Avatar:"));
		flexPanel.setWidget(3, 1, createAvatarPanel());
		
		keyHidden = new Hidden("accountKey");
		flexPanel.setWidget(4, 1, keyHidden);
		
		flexPanel.setWidget(5, 1, createSaveButton());
	}
	
	private Panel createAvatarPanel() {
		VerticalPanel avatarPanel = new VerticalPanel();
		
		avatar = new Image();
		avatar.setVisible(false);
		avatar.setStyleName("avatar");
		avatarPanel.add(avatar);
		
		diskInput = new FileUpload();
		diskInput.setName("avatarFile");
		avatarPanel.add(diskInput);
		
		deleteAvatar = new CheckBox("delete avatar");
		deleteAvatar.setName("deleteAvatar");
		deleteAvatar.setVisible(false);
		avatarPanel.add(deleteAvatar);
		
		return avatarPanel;
	}
	
	private Button createSaveButton() {
		Button button = new Button("Save", new ClickHandler() {
			public void onClick(ClickEvent event) {
				form.submit();
			}
		});
		
		form.addSubmitHandler(new FormPanel.SubmitHandler() {
			public void onSubmit(SubmitEvent event) {
				if (nameInput.getText().length() == 0) {
					GenericDialogBox dialog = new InformationDialogBox("Account");
					dialog.setMessage( "The name field must not be empty");
					dialog.showRelativeTo(nameInput);
					event.cancel();
				}
			}
		});

		form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
			public void onSubmitComplete(SubmitCompleteEvent event) {
				String result = event.getResults();
				if (result.length() != 0) {
					GenericDialogBox dialog = new ErrorDialogBox("Account");
					dialog.setMessage(result);
					dialog.center();
				} else {
					Window.Location.reload();
				}
			}
		});
		
		return button;
	}
	
	public Panel getPanel() {
		return form;
	}
	
	public String getKey() {
		return keyHidden.getValue();
	}

	public void setKey(String key) {
		keyHidden.setValue(key);
	}

	public void setEmail(String email) {
		emailLabel.setText(email);
	}

	public String getEmail() {
		return emailLabel.getText();
	}

	public void setAccountName(String account) {
		accountLabel.setText(account);
		nameInput.setText(account);
	}

	public String getAccountName() {
		return accountLabel.getText();
	}

	public void setName(String name) {
		nameInput.setText(name);
	}

	public String getName() {
		return nameInput.getText();
	}

	public void setAvatar(String avatarUrl) {
		avatar.setUrl(avatarUrl);
		avatar.setVisible(true);
		deleteAvatar.setVisible(true);
	}

}
